package com.small.util;

import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * @program: small
 * @description:ftp链连接和上传工具
 * @author: Mr.Xie
 * @create: 2019-04-25 11:54
 */
public class FTPUtil {
    private static final Logger logger= LoggerFactory.getLogger(FTPUtil.class);

    private static String ftpIP=PropertiesUtil.getProperty("ftp.server.ip");
    private static String ftpUser=PropertiesUtil.getProperty("ftp.user");
    private static String ftpPass=PropertiesUtil.getProperty("ftp.pass");

    private String ip;
    private int port;
    private String user;
    private String pass;
    private FTPClient ftpClient;

    private FTPUtil(String ip, int port, String user, String pass) {
        this.ip = ip;
        this.port = port;
        this.user = user;
        this.pass = pass;
    }

    public static boolean uploadFile(List<File> fileList) throws IOException {
        FTPUtil ftpUtil=new FTPUtil(ftpIP,21,ftpUser,ftpPass);
        //设置ftp连接数据
        boolean result=ftpUtil.uploadFile("image",fileList);
        logger.info("上传完毕，上传结果:{}",result);
        return result;
    }
    private boolean uploadFile(String remotePath ,List<File> fileList) throws IOException {
       boolean uploaded=true;
        FileInputStream fis=null;
        //调用判断是否连接登陆上ftp服务器
        if(connectServer(this.ip,this.port,this.user,this.pass)){
            try {
                ftpClient.changeWorkingDirectory(remotePath);//切换到目标文件夹
                ftpClient.setBufferSize(1024);//缓冲区1M
                ftpClient.setControlEncoding("UTF-8");
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);//设置文件为二进制格式
                // 每次数据连接之前，ftp client告诉ftp server开通一个端口来传输数据
                ftpClient.enterLocalPassiveMode();//打开本地被动模式
                for(File fileItem:fileList){
                    fis=new FileInputStream(fileItem);
                    ftpClient.storeFile(fileItem.getName(),fis);
                }
            } catch (IOException e) {
                logger.error("上传文件异常",e);
                uploaded=false;
                e.printStackTrace();
            }finally {
                fis.close();
                ftpClient.disconnect();
            }
        }
        return uploaded;
    }

    private boolean connectServer(String ip,int port,String user,String pwd){
        boolean isSuccess=false;
        ftpClient=new FTPClient();
        try {
            ftpClient.connect(ip,port);
            logger.info("开始连接ftp服务器");
            isSuccess=ftpClient.login(user,pwd);
            logger.info("登录ftp服务器，账号:{}",user);
        } catch (IOException e) {
            logger.error("ftp连接异常",e);
            e.printStackTrace();
        }
        return isSuccess;
    }




    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public FTPClient getFtpClient() {
        return ftpClient;
    }

    public void setFtpClient(FTPClient ftpClient) {
        this.ftpClient = ftpClient;
    }






}
